package com.company.zhishidian;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.Year;
import java.util.List;

public class XMLJX {
/**
 * 需要先导入lib里的jar包，在 File -》Project Sturcture -》 Libraies -》
 	点击加号 -》 选择 lib 所在文件夹 -》点击 OK -》选择 该项目名称 -》 OK 就可以导入了
 *
 * DOM 把整个文档映射成一个大的Document对象  （使用方便，内存占率高）
 * SAX 逐行解析 事件驱动 （使用不放变，性能好（内存应用少））
 */
}
// 解析xml
class ParseXML{
    public static void main(String[] args) throws DocumentException {
        File xmlFile = new File("pkg/xml/note.xml");
        SAXReader reader = new SAXReader();
        Document xmlDoc = reader.read(xmlFile);
        //  note
//        得到根元素
        Element root = xmlDoc.getRootElement();
//        获得根元素下的to元素内容
//        String toText = root.elementText("to");
//        System.out.println(toText);


        File xmlFile2 = new File("pkg/xml/bookstore.xml");
        SAXReader reader2 = new SAXReader();
        Document xmlDoc2 = reader.read(xmlFile);
        root = xmlDoc2.getRootElement();

        // bookstore
//        获得一组book元素
        List<Element> bookElList = root.elements();
        for (Element bookEl:bookElList) {
            String  value = bookEl.attributeValue("category");
            System.out.println(value);
            Element titlEl = bookEl.element("title");
            String  valueL = titlEl.attributeValue("lang");
            System.out.println(valueL);
            Element authorl = bookEl.element("author");
            Element yearl = bookEl.element("year");
            Element pricel = bookEl.element("price");
            System.out.println(titlEl.getName()+"="+titlEl.getText());
            System.out.println(authorl.getName()+"="+authorl.getText());
            System.out.println(yearl.getName()+"="+yearl.getText());
            System.out.println(pricel.getName()+"="+pricel.getText());
            System.out.println();
        }
    }
}
// 增加元素
class WriteXML{
    public static void main(String[] args) throws DocumentException, IOException {
        // 新建一个空白文档
//        Document document = DocumentHelper.createDocument();
        File xmlFile = new File("pkg/xml/bookstore.xml");
        SAXReader reader = new SAXReader();
        Document xmlDoc = reader.read(xmlFile);

        // 获取文档的根节点
        Element root = xmlDoc.getRootElement();
        // 在根节点下添加一个新的book节点
        Element bookEL = root.addElement("book");
        // 为book节点添加属性
        bookEL.addAttribute("category","HISTORY");
        // 以book节点做根节点在添加其他的子节点
        Element titleEL = bookEL.addElement("title");
        // 添加节点文本
        titleEL.setText("HongLou Dream");
        titleEL.addAttribute("lang","zh");
        Element authorEL = bookEL.addElement("author");
        authorEL.setText("CaoXueQin");
        Element yearEL = bookEL.addElement("year");
        yearEL.setText("1988");
        Element priceEL = bookEL.addElement("price");
        priceEL.setText("50.00");

        // 格式化xml文档
        OutputFormat format = OutputFormat.createPrettyPrint();
        // 编码格式设置
        format.setEncoding("GBK");
        // 按格式写入文件
        FileWriter fw = new FileWriter("pkg/bookstore.xml");
        XMLWriter writer = new XMLWriter(fw,format);
        // 把包括根节点在内的所有节点放进去
        writer.write(xmlDoc);
        writer.close();

    }
}
// 更新修改
class UpdateXml{
    public static void main(String[] args) throws DocumentException, IOException {
        File xmlFile = new File("pkg/xml/bookstore.xml");
        SAXReader reader = new SAXReader();
        Document xmlDoc = reader.read(xmlFile);

        Element root = xmlDoc.getRootElement();
        List<Element> bookElList=root.elements();
        for (Element bookEl:bookElList) {
//           修改元素
            if(Integer.parseInt(bookEl.elementText("year"))<2004){
                bookEl.element("price").setText(
                        Float.parseFloat(
                                bookEl.elementText("price")
                        )+5+""
                );
            }
            // 删除元素
            if(bookEl.elementText("title").equals("HongLou Dream")){
                root.remove(bookEl);
            }
        }
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        FileWriter fw = new FileWriter("pkg/xml/bookstore.xml");
        XMLWriter writer = new XMLWriter(fw,format);
        writer.write(xmlDoc);
        writer.close();
    }
}
// 查询
class XPath{
    public static void main(String[] args) throws DocumentException {
        File xmlFile = new File("pkg/xml/bookstore.xml");
        SAXReader reader = new SAXReader();
        Document xmlDoc = reader.read(xmlFile);
        // 查询文本内容
        List<Element> bookElList = xmlDoc.selectNodes("//bookstore/book[price>40]");
        System.out.println(bookElList.get(0).elementText("title"));
        // 查询元素
        bookElList = xmlDoc.selectNodes("//@lang");
        System.out.println(bookElList.size());
    }
}